import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2025-10-05
 * Time: 22:55
 */
public class Test102 {

    public static void nextPermutation(int[] num) {
        int last = -1, n = num.length;
        // [1,3,5,6,2,4]
        for(int i = n-1; i > 0; i--) {
            if(num[i] > num[i-1]) {
                last = i-1;
                break;
            }
        }
        if(last != -1) {
            int change = last+1;
            for(int i = last; i < n; i++) {
                if(num[i] > num[last]) {
                    change = i;
                }else {
                    break;
                }
            }
            swap(num,change,last);
            for(int i = last; i < (n-last)/2; i++) {
                swap(num,i+last,n-i);
            }
        }else {
            for(int i = 0; i < n / 2; i++) {
                swap(num,i,n-i-1);
            }
        }
    }

    public static void swap(int[] num, int i, int j) {
        int tmp = num[i];
        num[i] = num[j];
        num[j] = tmp;
    }

    public static void main(String[] args) {
        int[] num = {1,2,3};
        nextPermutation(num);
        System.out.println(Arrays.toString(num));
    }
}
